package com.stormrage.edu.dao.ent.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.stormrage.edu.dao.ent.EntAcctDao;
import com.stormrage.entity.ent.EntAcct;
import com.stormragetech.core.dao.MysBaseDao;
import com.stormragetech.core.exception.StormrageTechException;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Repository
public class EntAcctDaoImpl extends MysBaseDao implements EntAcctDao {

	private static final Logger logger = LoggerFactory.getLogger(EntAcctDaoImpl.class);

	@Override
	public Integer create(EntAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 Create 】- 开始运行 ");

		final String query = "INSERT INTO ent_acct (acct, pass, salt, ent_id, real_name, reg_date, locked) VALUES"
				+ " (:acct, :pass, :salt, :entId, :realName, :regDate, :locked)";
		logger.debug(" 数据库执行语句为 " + query);

		Integer index = create(query, arg0);

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(EntAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 Update 】- 开始运行 ");

		final String query = "UPDATE ent_acct SET acct = :acct, pass = :pass, salt = :salt, ent_id = :entId,"
				+ " real_name = :realName, reg_date = :regDate, locked = :locked WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);
		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void updatePass(EntAcct arg0) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 UpdatePass 】- 开始运行 ");

		final String query = "UPDATE ent_acct SET pass = :pass, salt = :salt WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		update(query, arg0);
	}

	@Override
	public EntAcct queryById(Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryById 】- 开始运行 ");

		final String query = "SELECT * FROM ent_acct WHERE id = :id";
		logger.debug(" 数据库执行语句为 " + query);

		EntAcct entity = queryObject(query, EntAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		return entity;
	}

	@Override
	public EntAcct queryByIdLock(Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryByIdLock 】- 开始运行 ");

		final String query = "SELECT * FROM ent_acct WHERE id = :id AND locked = :locked";
		logger.debug(" 数据库执行语句为 " + query);

		EntAcct entity = queryObject(query, EntAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		return entity;
	}

	@Override
	public EntAcct queryByAcct(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryByAcct 】- 开始运行 ");

		final String query = "SELECT * FROM ent_acct WHERE acct = :acct";
		logger.debug(" 数据库执行语句为 " + query);

		EntAcct entity = queryObject(query, EntAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		return entity;
	}

	@Override
	public EntAcct queryByAcctLock(final Map<String, Object> map) throws StormrageTechException {

		logger.debug(" 【 Dao 】- 【 EntAcctDao 】- 【 QueryByAcctLock 】- 开始运行 ");

		final String query = "SELECT * FROM ent_acct WHERE acct = :acct AND locked = :locked";
		logger.debug(" 数据库执行语句为 " + query);

		EntAcct entity = queryObject(query, EntAcct.class, map);
		logger.debug(" 查询结果为 " + entity);

		return entity;
	}

}
